package cn.wolfcode.Controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.service.PermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private INoticeService noticeService;

    @Autowired
    private PermissionService permissionService;
    @RequestMapping("/UserLogin")
    @ResponseBody
     public jsonResult login(Employee employee, HttpSession httpSession){

            Employee e = employeeService.cheeckLogin(employee.getUsername(), employee.getPassword());
            //将用户信息存在session中
            httpSession.setAttribute(UserContext.USER_IN_SESSION, UserContext.getCurrentEmployee());
            //将用户拥有的权限信息存入session中
            List<String> list = permissionService.selecByEmployeeId(e.getId());
            UserContext.setCurrentEmployee(e);
            UserContext.setCurrentExpermission(list);

        return  new jsonResult();
    }


    //注销
    //将用户的session的清除掉，跳转到登录页面
    @RequestMapping("/Logout")
    public String Logout(Employee employee, HttpSession session){
        //将当前的session信息全部清空
            session.invalidate();
        return "redirect:/login.html";
    }
}
